import { Global, Module } from '@nestjs/common';
import { DbService } from './db.service';
import { MemberRepositoryModule } from '@app/db/common/member-repository/member-repository.module';
import { MerchantRepositoryModule } from '@app/db/merchant-repository/merchant-repository.module';
import { MenuRepositoryModule } from '@app/db/common/menu-repository/menu-repository.module';
import { RoleRepositoryModule } from '@app/db/common/role-repository/role-repository.module';
import { DictionaryCategoryRepositoryModule } from '@app/db/common/dictionary-category-repository/dictionary-category-repository.module';
import { DictionaryItemRepositoryModule } from '@app/db/common/dictionary-item-repository/dictionary-item-repository.module';
import { DeviceTypeRepositoryModule } from '@app/db/device-type-repository/device-type-repository.module';
import { DeviceRepositoryModule } from '@app/db/device-repository/device-repository.module';
import { DeviceParamsFieldRepositoryModule } from '@app/db/device-params-field-repository/device-params-field-repository.module';
import { DeviceParamsRepositoryModule } from '@app/db/device-params-repository/device-params-repository.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigureModule, ConfigureService } from '@app/configure';
import { SysConfigModule } from '@app/db/common/sys-config/sys-config.module';

@Global()
@Module({
  imports : [
    TypeOrmModule.forRootAsync({
		imports : [ConfigureModule],
		inject : [ConfigureService],
		useFactory : (config:ConfigureService) => {
			return {
				type: config.get('DB_TYPE'),
				host: config.get('DB_HOST'),
				port: Number(config.get('DB_PORT')),
				database: config.get('DB_NAME'),
				username: config.get('DB_USERNAME'),
				password: config.get('DB_PASSWORD'),
				synchronize: JSON.parse(config.get('DB_SYNCHRONIZE')),
				autoLoadEntities : JSON.parse(config.get('DB_AUTOLOADENTITIES')),
				poolSize : Number(config.get('DB_POOLSIZE'))
			}
		},
	
		// dataSourceFactory: async (options) => {
		// 	const dataSource = await new DataSource(options).initialize();
		// 	return dataSource;
		// },
	}),
    MemberRepositoryModule,
    MenuRepositoryModule,
    RoleRepositoryModule,
    DictionaryCategoryRepositoryModule,
    DictionaryItemRepositoryModule,
    SysConfigModule,

    MerchantRepositoryModule,
    DeviceTypeRepositoryModule,
    DeviceRepositoryModule,
    DeviceParamsFieldRepositoryModule,
    DeviceParamsRepositoryModule,
  ],
  providers: [DbService],
  exports : [DbService]
})
export class DbModule {}
